﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.Code;
using Sausage.Web.DataModel;
using Sausage.Web.Domain;
using Sausage.Web.Models;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 养护机械控制器
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2012/02/22 12:23    创建
    /// </history>
    public class MaintainMachineController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Submit()
        {
            return View();
        }

        /// <summary>
        /// 审核。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Audit()
        {
            return View();
        }

        /// <summary>
        /// 确认。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Confirm()
        {
            return View();
        }

        /// <summary>
        /// 查看养护机械。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewQuantityMachine(Guid quantityID)
        {
            //返回视图
            return View(MaintainQuantityHelper.GetMaintainQuantityByID(quantityID));
        }

        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程项目信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = MaintainMachineHelper.Select(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护机械加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程项目信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult AuditSelect(JosnPagingQueryModel query)
        {
            var result = MaintainMachineHelper.AuditSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护机械加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }


        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程项目信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SubmitSelect(JosnPagingQueryModel query)
        {
            var result = MaintainMachineHelper.SubmitSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护机械加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }


        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的工程项目信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ConfirmSelect(JosnPagingQueryModel query)
        {
            var result = MaintainMachineHelper.ConfirmSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护机械加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid id)
        {
            var item = MaintainMachineHelper.GetMaintainMachineByID(id);
            if (item == null)
                return Json(new JsonResultViewModel { success = true, message = "找不到此养护机械！" },
                            JsonRequestBehavior.AllowGet);

            var result = new
                             {
                                 MaintainMachineID = item.MaintainMachineID,
                                 QuantityID = item.QuantityID,
                                 OrganizationID = item.OrganizationID,
                                 Organization = item.Organization.OrganizationName,
                                 MachineID = item.MachineID,
                                 Machine = item.Machine.ItemName,
                                 MachineTypeID = item.MachineTypeID,
                                 MachineType = item.MachineType.ItemName,
                                 MachineUnitID = item.MachineUnitID,
                                 OilTypeID = item.OilType,
                                 item.OilWear,
                                 item.OilPrice,
                                 item.OilAmount,
                                 item.MaintenanceCost,
                                 MaintainCode = item.MaintainQuantity.MaintainCode,
                                 Quantity = item.Quantity,
                                 Price = item.Price,
                                 Amount = item.Amount,
                                 CreateDate = item.CreateDate,
                                 Applicant = item.Applicant,
                                 ApplicationDate = item.ApplicationDate,
                                 Auditor = item.Auditor,
                                 AuditedDate = item.AuditedDate,
                                 Approver = item.Approver,
                                 ApprovedDate = item.ApprovedDate,
                                 State = item.State,
                                 IsDelay = item.IsDelay,
                                 Memo = item.Memo
                             };


            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    total = 1,
                    data = result,
                    message = "加载养护机械成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(MaintainMachineListRow data)
        {
            MaintainMachineHelper.DeleteMaintainMachine(data.MaintainMachineID);
            return Json(new JsonResultViewModel { success = true, message = "养护机械删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(MaintainMachineListRow data)
        {
            MaintainMachineHelper.UpdateMaintainMachine(data);
            return Json(new JsonResultViewModel { success = true, message = "养护机械更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult UpdateState(Guid id, int state)
        {
            MaintainMachineHelper.UpdateMaintainMachineState(id, state);
            return Json(new JsonResultViewModel { success = true, message = "养护机械状态更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <returns>创建结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(MaintainMachineListRow data)
        {
            string errorMessage;
            if (!Helper.CouldSet(data.ApplicationDate, out errorMessage))
                return Json(new JsonResultViewModel { success = false, message = errorMessage }, JsonRequestBehavior.AllowGet);

            MaintainMachineHelper.CreateMaintainMachine(data);
            return Json(new JsonResultViewModel { success = true, message = "养护机械创建成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="quantityID">The quantity ID.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSubmit(Guid[] machines)
        {

            MaintainMachineHelper.ExecuteSubmit(machines);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护机械提交成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="machines">The machines.</param>
        /// <param name="name">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteAudit(Guid[] machines, bool passed, string comment)
        {
            MaintainMachineHelper.ExecuteAudit(machines, passed, comment);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护机械审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 批准
        /// </summary>
        /// <param name="machines">The machines.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteConfirm(Guid[] machines, bool passed, string comment)
        {
            MaintainMachineHelper.ExecuteConfirm(machines, passed, comment);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护机械审核成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
